package com.ymatou.iapi.dguser.testcase;

import com.google.gson.JsonObject;
import com.ymatou.iapi.dguser.parameter.OpenIdInfoBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.service.OpenIdInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
import org.junit.*;
import org.junit.experimental.categories.Category;
/**
*根据openid获取用户绑定信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OpenIdInfo{
	private static OpenIdInfoBean openidinfoBean;
	private static OpenIdInfoCall openidinfoCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据openid获取用户绑定信息");
	}

	@Before
	public void caseUp() {
		openidinfoBean=new OpenIdInfoBean();
		openidinfoCall=new OpenIdInfoCall();
		DGUserWapper = new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(openidinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(String openId) {
		DGThirdPartyBindInfo dgThirdPartyBindInfo = DGUserWapper.selectDGThirdPartyOpenId(openId).get(0);
		JsonObject jet = openidinfoCall.getJsonObject().get("userBindDto").getAsJsonObject();
		Logger.verifyEquals(jet.get("bindId").getAsInt(),dgThirdPartyBindInfo.getBindId(),"验证bindId");
		Logger.verifyEquals(jet.get("userType").getAsInt(),dgThirdPartyBindInfo.getUserType(),"验证userType");
		Logger.verifyEquals(jet.get("thirdPartyType").getAsInt(),dgThirdPartyBindInfo.getThirdPartyType(),"验证thirdPartyType");
		Logger.verifyEquals(jet.get("appSource").getAsInt(),dgThirdPartyBindInfo.getAppSource(),"验证appSource");
		Logger.verifyEquals(jet.get("unionId").getAsString(),dgThirdPartyBindInfo.getUnionId(),"验证unionId");
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenIdInfo_001() {
		Logger.start(true,"根据openid获取用户绑定信息成功");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setMobile(TestDataManager.getPhoneNum());
			DGUserCallService.UserCreateCall(usercreateBean);
			openidinfoBean.setOpenId(usercreateBean.getOpenId());
			openidinfoBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			openidinfoCall.setData(openidinfoBean);
			openidinfoCall.callService();
			Logger.verifyEquals("true", openidinfoCall.getString("success"), "验证success");
			verifyDB(openidinfoBean.getOpenId());
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_OpenIdInfo_002() {
		Logger.start(true,"不输入openid失败");
		try {
			openidinfoBean.setOpenId(null);
			openidinfoBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			openidinfoCall.setData(openidinfoBean);
			openidinfoCall.callService();
			Logger.verifyEquals("false", openidinfoCall.getString("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_OpenIdInfo_003() {
		Logger.start(true,"不输入WxAppName失败");
		try {
			openidinfoBean.setOpenId("123");
//			openidinfoBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			openidinfoCall.setData(openidinfoBean);
			openidinfoCall.callService();
			Logger.verifyEquals("false", openidinfoCall.getString("success"), "验证success");

		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenIdInfo_004() {
		Logger.start(true,"根据openid获取商家用户绑定信息成功");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initSellerUsercreateBean();
			usercreateBean.setMobile(TestDataManager.getPhoneNum());
			DGUserCallService.UserCreateCall(usercreateBean);
			openidinfoBean.setOpenId(usercreateBean.getOpenId());
			openidinfoBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			openidinfoCall.setData(openidinfoBean);
			openidinfoCall.callService();
			Logger.verifyEquals("true", openidinfoCall.getString("success"), "验证success");
			verifyDB(openidinfoBean.getOpenId());
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenIdInfo_005() {
		Logger.start(true,"根据openid获取用户绑定信息成功独立小程序");
		try {
			UserCreateBean usercreateBean= DGUserCallService.initUsercreateBean();
			usercreateBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usercreateBean.setWxAppName(DGUserCallService.TestWxsqOther);
			usercreateBean.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			DGUserCallService.UserCreateCall(usercreateBean);
			openidinfoBean.setOpenId(usercreateBean.getOpenId());
			openidinfoBean.setWxAppName(DGUserCallService.TestWxsqOther);
			openidinfoCall.setData(openidinfoBean);
			openidinfoCall.callService();
			Logger.verifyEquals("true", openidinfoCall.getString("success"), "验证success");
			verifyDB(openidinfoBean.getOpenId());
			Integer belongSellerId = openidinfoCall.getJsonObject().get("userBindDto").getAsJsonObject().get("belongSellerId").getAsInt();
			Logger.verifyEquals(DGUserCallService.TestWxsqOtherSellerId, belongSellerId, "验证belongSellerId");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
}